package com.tebiecloud.yunst.server.service.impl;

import com.tebiecloud.common.utils.DateUtils;
import com.tebiecloud.yunst.client.model.entity.YunWithdrawApply;
import com.tebiecloud.yunst.client.model.vo.BandCardVO;
import com.tebiecloud.yunst.server.mapper.YunWithdrawApplyMapper;
import com.tebiecloud.yunst.server.service.YunWithdrawApplyService;
import com.tebiecloud.common.mybatis.base.service.impl.BaseServiceImpl;
import com.tebiecloud.yunst.server.yunstservice.YunStService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *  服务实现类
 * @author tebie
 * @date 2019-09-07
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class YunWithdrawApplyServiceImpl extends BaseServiceImpl<YunWithdrawApplyMapper, YunWithdrawApply> implements YunWithdrawApplyService {

    @Value("${tebiecloud.yunst.backUrl}")
    private String backUrl;

//    @Value("tebiecloud.yunst.withdrawFee")
//    private String withdrawFee;

//    @Value("tebiecloud.yunst.withdrawTax")
//    private int withdrawTax;

    @Autowired
    YunStService yunStService;

    @Autowired
    YunWithdrawApplyMapper yunWithdrawApplyMapper;

    @Override
    public Map<String, Object> yunwithdrawApply(long userId,String bizUserId, int amount, String cardNo) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Long amountlong = new Long((long)amount);
        int fee = 100;//默认是1块钱手续费
        int tax = 0;
        String newbackUrl = backUrl+ "/order/ordercallback/withdrawCallBack";
        Map<String, String> withdrawMap =  yunStService.withdrawApply(bizUserId,amountlong,fee+tax,newbackUrl,cardNo);
        if(withdrawMap.get("status").equals("error")){
            resultMap.put("status","error");
            resultMap.put("message",withdrawMap.get("message"));
            return resultMap;
        }
        Date nowDate = new Date();
        YunWithdrawApply yunWithdrawApply = new YunWithdrawApply();
        yunWithdrawApply.setAmount(amount);
        yunWithdrawApply.setFee(fee);
        yunWithdrawApply.setTax(tax);
        yunWithdrawApply.setUserId(userId);
        yunWithdrawApply.setApplyTime(nowDate);
        yunWithdrawApply.setArrivedTime(DateUtils.getAddDate(new Date(), Calendar.MINUTE,5));
        yunWithdrawApply.setBankCardNo(cardNo);
        yunWithdrawApply.setBizOrderNo(withdrawMap.get("bizOrderNo"));
        yunWithdrawApply.setBizUserId(bizUserId);
        yunWithdrawApply.setStatus(1);
        yunWithdrawApply.setRemark(withdrawMap.get("res"));
        yunWithdrawApplyMapper.insert(yunWithdrawApply);
        resultMap.put("status","OK");
        resultMap.put("yunWithdrawApply",yunWithdrawApply);
        return resultMap;
    }
}
